package com.lindroy.lib_utils

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import java.lang.reflect.InvocationTargetException

/**
 * @author Lin
 * @date 2021/4/6
 * @function
 */
@SuppressLint("PrivateApi")
internal object AndUtil {

    val application by lazy {
        var app: Application? = null
        try {
            app = Class.forName("android.app.ActivityThread")
                .getMethod("currentApplication")
                .invoke(null) as Application
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: InvocationTargetException) {
            e.printStackTrace()
        } catch (e: NoSuchMethodException) {
            e.printStackTrace()
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        }
        if (app == null) {
            throw NullPointerException("AndUtil application is null")
        }
        app!!
    }


    val appContext: Context by lazy { application.applicationContext }
}